package com.ant.myagile.dashboard
import com.ant.myagile.*
import com.ant.myagile.backlog.*
import grails.converters.JSON
import grails.plugins.springsecurity.*

class DashboardController {
	def springSecurityService
	def statusService
	def issueService
	def teamService
	def sprintService
	
	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	def index() { 
		if (params.sort == null)
		{
			params.sort = 'id'
			params.order = 'desc'
		}
		Member owner = springSecurityService.currentUser
		List<Team> teams = teamService.getAllTeamByUser(owner.id)
//		[statusList: Status.list(params), teamList : teams]
		render (template:'index',model:[statusList: Status.list(params), teamList : teams])
	}
	
	def kanban(){		
		List<Status> statuslist = statusService.getStatusByTeamSprint(params, request, servletContext)		
		Status start = new Status()
		Status done = new Status()
		statuslist.sort{it.id}
		for(i in statuslist){
			if(i.start){
				start = i
			}
			else if(i.done){
				done = i
				break
			}
		}
		statuslist.remove(statuslist.indexOf(start))
		statuslist.remove(statuslist.indexOf(done))
//		
//		[statusList: statuslist, start: start, done: done, teamid: params.teamid]
		render (template:'kanban',model:[statusList: statuslist, start: start, done: done, teamid: params.teamid])
		
	}
	
	def getSprintAndTeamMember(){
		Set<Member> memberList = teamService.getMembersByTeamId(params.teamid)
		Set<Sprint> sprintList = sprintService.findSprintByTeamId(params.teamid)
		def lastSprint = sprintService.findLastSprintByTeamId(params.teamid)
		
		render([sprintList:sprintList, lastSprint: lastSprint, memberList: memberList] as JSON)
	}
	
	def updateStatus(){
		def json = request.JSON
		def teamid = json[0].teamid
		def sprintid = json[0].sprintid
		int i = 0
		json.each{
			if(!i){
				i=1;
			}
			else if(it.status == 'new'){
				Status st = new Status(name: it.name, color: it.color, hidden: false, start: false, done: false, team: Team.get(teamid), sprint: Sprint.get(sprintid))
				st.save()
			}else if(it.status == 'remove'){
				if(it.prevstatus!=null){
					Status st = Status.get(it.id)
					Status prevst = Status.get(it.prevstatus)
					def query = Issue.where {
					    status == st
					}
					int total = query.updateAll(status: prevst)
				}
				Status st = Status.get(it.id)
				st.delete()
			}else{
				Status st = Status.get(it.id)
				st.name = it.name
				st.color = it.color
				st.save()			
			}
		}
		render ''		
	}
	
	def updateStatusIssue(){
		Issue issue = Issue.get(params.issueid)
		Status status = Status.get(params.statusid)
		issue.status = status
		issue.save()
		render ''
	}
	
	def assignMemberForIssue(){
		Issue issue = Issue.get(params.issueID)
		if(params.memberID!='null'){
			Member mem = Member.get(params.memberID)
			issue.assigned = mem
			issue.save()
			render mem as JSON
		}else{
			issue.assigned = null
			render mem as JSON
		}		
		
	}
	
	def getIssueBySprintID(){
		def issueList = issueService.findIssueBySprintId(params.sprintID)
		def issueMap = [:]	
		
		issueList.each {
			Member assigned= new Member()
			if(it.assigned){
				assigned = Member.get(it.assigned.id)				
			}
			issueMap.put(it.id, assigned)
		}
		render([issueList: issueList.sort(), memberAssigned: issueMap] as JSON)
	}
}
